package com.ruoyi.reservoir.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.reservoir.domain.Image;
import com.ruoyi.reservoir.domain.dto.ParamDto;
import com.ruoyi.reservoir.domain.vo.ImageTimeAxisVo;
import com.ruoyi.reservoir.domain.vo.TimeSequenceAreaVo;
import com.ruoyi.reservoir.mapper.ImageMapper;
import com.ruoyi.reservoir.service.IImageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@DS("slave")
@RequiredArgsConstructor
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements IImageService {

    /**
     * 获取首页驾驶舱影像时间轴列表
     * @return
     */
    @Override
    public List<ImageTimeAxisVo> getImageTimeAxis() {

        return lambdaQuery()
            .orderByAsc(Image::getImageTime)
            .list()
            .stream()
            .map(e -> BeanUtil.toBean(e , ImageTimeAxisVo.class))
            .collect(Collectors.toList());
    }

    /**
     * 获取首页面积时序分析统计结果
     * @return
     */
    @Override
    public TimeSequenceAreaVo getTimeSequenceArea() {

        //获取影像列表
        List<Image> images = lambdaQuery()
            .orderByAsc(Image::getImageTime)
            .list();

        List<String> times = new ArrayList<>();
        List<ParamDto> series = new ArrayList<>();

        List<String> nameList = Arrays.asList("库区水域面积", "消落区面积", "孤岛面积");
        for (String name : nameList){
            ParamDto dto = new ParamDto(name, new ArrayList<>());
            series.add(dto);
        }

        /*
            封装面积结果
         */
        images.forEach(e -> {
            times.add(e.getImageTime());
            //库区水域面积
            series.get(0).getData().add(e.getReservoirArea());
            //消落区面积
            series.get(1).getData().add(e.getRiparianzoneArea());
            //孤岛面积
            series.get(2).getData().add(e.getIslandArea());
        });

        return new TimeSequenceAreaVo(times, series);
    }

    /**
     * 根据idSet封装影像Map结果
     * @param imageIdSet
     * @return
     */
    @Override
    public Map<String, Image> getImageMap(Set<String> imageIdSet) {
        if (imageIdSet.isEmpty()){
            return new HashMap<>();
        }

        return lambdaQuery()
            .in(Image::getId, imageIdSet)
            .list()
            .stream()
            .collect(Collectors.toMap(Image::getId, e -> e));
    }
}
